package com.example.hyw.szysteam.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.example.hyw.szysteam.POJO.UserInfo;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.presenter.UserInfoPresenter;
import com.example.hyw.szysteam.utils.ShowTips;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

/**
 * Created by HYW on 2018/6/6.
 */

public class EditInfoActivity extends AppCompatActivity {
    QMUITopBar mTopBar;
    EditText editName;
    EditText editSchool;
    RadioGroup editSex;
    EditText editAge;
    private UserInfoPresenter userInfoPresenter=new UserInfoPresenter(this);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editinfo);
        initTopBar();
        initView();
    }

    private void initTopBar(){
        mTopBar=findViewById(R.id.topbar);
        mTopBar.addLeftImageButton(R.drawable.ic_left,R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTopBar.addRightTextButton("保存",R.id.right).setTextColor(getResources().getColor(R.color.myWhite));
        View rightButton=findViewById(R.id.right);
        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean sex=true;
                if(editSex.getCheckedRadioButtonId()==R.id.male){
                    sex=false;
                }
                userInfoPresenter.editInfo(UserInfo.getEmail(),editName.getText().toString(),editSchool.getText().toString(),sex,Integer.parseInt(editAge.getText().toString()));
            }
        });
        mTopBar.setTitle("修改信息").setTextColor(getResources().getColor(R.color.myWhite));
    }

    private void initView(){
        editName=findViewById(R.id.edit_name);
        editSchool=findViewById(R.id.edit_school);
        editSex=findViewById(R.id.edit_sex);
        editAge=findViewById(R.id.edit_age);
        editName.setText(UserInfo.getName());
        editSchool.setText(UserInfo.getSchool());
        if(UserInfo.isSex()){
            editSex.check(R.id.famale);
        }else {
            editSex.check(R.id.male);
        }
        editAge.setText(""+UserInfo.getAge());
    }

    public void editSuccess(){
        ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_SUCCESS,"修改成功",mTopBar,this);
    }
}
